package com.jiangyg.devtools.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.jiangyg.devtools.toolkit.Assert;
import com.jiangyg.devtools.toolkit.AwtUtils;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

/**
 * 类描述：抽象弹窗
 *
 * @author jiangyg
 * @date 2023-08-17
 */
public abstract class AbstractDialogWrapper extends DialogWrapper {

    protected final Project project;

    protected AbstractDialogWrapper(@Nullable Project project) {
        super(project);

        this.project = project;
    }

    /**
     * 功能描述：通过比率设置窗口的大小
     *
     * @param ratio 比率
     */
    protected void setSizeByRatio(double ratio) {
        setSizeByRatio(ratio, ratio);
    }

    /**
     * 功能描述：通过比率设置窗口的大小
     *
     * @param wratio 比率
     * @param hratio 比率
     */
    protected void setSizeByRatio(double wratio, double hratio) {
        Assert.state(wratio <= 1);

        final Dimension dimension = AwtUtils.getScreenSize();

        final int width = Double.valueOf(dimension.getWidth() * wratio).intValue();
        final int height = Double.valueOf(dimension.getHeight() * hratio).intValue();

        this.setSize(width, height);

        final int x = Double.valueOf((dimension.getWidth() - width) / 2).intValue();
        final int y = Double.valueOf((dimension.getHeight() - height) / 2).intValue();

        this.setLocation(x, y);
    }

}
